package com.example.a28796.luxury;

import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a28796.luxury.fragment.FragmentBestProduct;
import com.example.a28796.luxury.fragment.FragmentClassfy;
import com.example.a28796.luxury.fragment.FragmentFirstPage;
import com.example.a28796.luxury.fragment.FragmentMine;
import com.example.a28796.luxury.fragment.FragmentShoppingPage;


public class MainActivity extends AppCompatActivity {
    private FragmentManager mManager;
    private RadioGroup mRadioGroup;
    private Fragment mCurrentFragment;
    private NavigationView mNavigationView;
    private DrawerLayout mDrawerLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
        setListener();
    }
    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                //弹出提示，可以有多种方式
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
    protected void initView() {
        ActionBar supportActionBar = getSupportActionBar();
        if(supportActionBar!=null){
            supportActionBar.hide();
        }
        mDrawerLayout = (DrawerLayout) findViewById(R.id.dl_navigation_test);
        mNavigationView = (NavigationView) findViewById(R.id.nv_navigation_test);
        mRadioGroup = (RadioGroup) findViewById(R.id.rg_main_index);
        showAndHide(R.id.fl_main_content,FragmentFirstPage.class);
        //第一步 给导航栏NavigationView设置点击事件s
        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.item_settings:
                        Toast.makeText(MainActivity.this, "男士", Toast.LENGTH_SHORT).show();
                        mDrawerLayout.closeDrawers();
                        break;
                    case R.id.item_music:
                        Toast.makeText(MainActivity.this, "女士", Toast.LENGTH_SHORT).show();
                        mDrawerLayout.closeDrawers();
                        break;
                    case R.id.item_sound:
                        Toast.makeText(MainActivity.this, "。。。", Toast.LENGTH_SHORT).show();
                        mDrawerLayout.closeDrawers();
                        break;
                    default:
                        break;
                }
                return false;
            }
        });
        //第二步 拿到头布局然后设置点击事件
        //获取头布局
        View headerView = mNavigationView.getHeaderView(0);
        if(headerView!=null){
            TextView textView = (TextView) headerView.findViewById(R.id.tv_header_test);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "女朋友没了", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    protected void setListener() {
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Class<? extends Fragment> clazz = null;
                switch (checkedId) {
                    case R.id.rb_main_first:
                        //首页
                        clazz = FragmentFirstPage.class;
                        break;
                    case R.id.rb_main_bestproduct:
                        //尖货
                        clazz = FragmentBestProduct.class;
                        break;
                    case R.id.rb_main_classfy:
                        //分类
                        clazz = FragmentClassfy.class;
                        break;
                    case R.id.rb_main_shopping:
                        //购物袋
                        clazz = FragmentShoppingPage.class;
                        break;
                    case R.id.rb_main_mine:
                        //我的
                        clazz = FragmentMine.class;
                        break;
                    default:
                        break;
                }
                showAndHide(R.id.fl_main_content, clazz);
            }
        });
    }

    private void showAndHide(int contentId, Class<? extends Fragment> clazz){

        if(mCurrentFragment!=null&&mCurrentFragment.getClass().getSimpleName().equals(clazz.getSimpleName())){
            return;
        }
        FragmentTransaction transaction = mManager.beginTransaction();
        Fragment fragment = null;
        try {

            Fragment fragmentByTag = mManager.findFragmentByTag(clazz.getSimpleName());
            if(fragmentByTag!=null){
                transaction.show(fragmentByTag);
                transaction.hide(mCurrentFragment);
                mCurrentFragment = fragmentByTag;
            }else{
                fragment = clazz.newInstance();
                transaction.add(contentId,fragment,clazz.getSimpleName());
                if(mCurrentFragment!=null){
                    transaction.hide(mCurrentFragment);
                }
                mCurrentFragment = fragment;
            }
            transaction.commit();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }
    protected void initData() {
        mManager = getSupportFragmentManager();
    }

}


